from django.contrib.auth import logout
from django.shortcuts import render, redirect
from django.views import View
from goods.models import GoodsChannel
from contents.models import ContentCategory
from meiduo_mall.utils.my_category import get_categories


class IndexView(View):
    def get(self, request):
        categories = get_categories()
        content_categories = ContentCategory.objects.order_by('id').all()
        contents = {}
        for content_category in content_categories:
            # 一对多
            contents[content_category.key] = content_category.content_set.all()
        context = {
            "categories": categories,
            "contents": contents
        }
        return render(request, 'index.html', context=context)


class LogoutView(View):
    def get(self, request):
        # 清除seeion
        logout(request)
        response = redirect('/')
        # 清除cookie
        response.delete_cookie('username')
        return response

 # 主页小图标
class LogoView(View):
    def get(self, request):
        return redirect("/static/favicon.ico")

# Create your views here.
